package com.bawei.newsapplication.view.activity.home

import android.content.Intent
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.ayvytr.adapter.smart
import com.bawei.newsapplication.R
import com.bawei.newsapplication.base.BaseActivity
import com.bawei.newsapplication.databinding.ActivitySearchBinding
import com.bawei.newsapplication.view.adapter.DatabaseHelper
import com.bawei.newsapplication.viewmodel.HomeViewModel
import com.bawei.shixun.view.adapter.LeiAdapter

/**
 * 作者: 谢庆平
 * 时间: 2024/10/22 9:07
 * 描述:
 */
class SearchActivity : BaseActivity<HomeViewModel,ActivitySearchBinding>(R.layout.activity_search) {
    //TODO 搜索框数据
    var searchAdapter=smart(mutableListOf<String>(),R.layout.search_item,{
        item, position ->
        findViewById<TextView>(R.id.searchTilte).text=item
    }){}
    override fun initView() {

        //TODO 搜索框数据数据库数据
        var databaseHelper =DatabaseHelper(this)
        var sosuo=mDataBinding.searchEdit.findViewById<TextView>(R.id.search_edit)
        var search=mDataBinding.searchText.findViewById<TextView>(R.id.search_text)

        var queryData=databaseHelper.queryData()
        searchAdapter.update(queryData)

        search.setOnClickListener {
           var sosuo1=sosuo.text.toString()
            databaseHelper.insertData(sosuo1)
            var queryData=databaseHelper.queryData()
            searchAdapter.update(queryData)
            startActivity(Intent(this,ResuoutActivity::class.java).apply {
                    putExtra("name",sosuo1)
            })
           sosuo.text=""
        }
        mDataBinding.recylerSearch.adapter=searchAdapter
        mDataBinding.recylerSearch.layoutManager=androidx.recyclerview.widget.LinearLayoutManager(this)
        mDataBinding.queryText.setOnClickListener {
            databaseHelper.cleardata()
            searchAdapter.clear()
        }
//        if (searchAdapter.isEmpty()){
//
//            mDataBinding.recylerSearch.visibility=android.view.View.GONE
//        }else{
//            mDataBinding.recylerSearch.visibility=android.view.View.VISIBLE
//
//        }
    }

    override fun dataObserve() {
//        if (it.result.isEmpty()){
//            mDataBinding.recylerSearch.visibility= View.GONE
//            mDataBinding.llEmpty.visibility= View.VISIBLE
//        }else{
//            mDataBinding.recylerSearch.adapter=this?.let { ite1 -> LeiAdapter(ite1,it.result) }
//            mDataBinding.recylerSearch.layoutManager= LinearLayoutManager(this)
//            mDataBinding.recylerSearch.visibility= View.VISIBLE
//            mDataBinding.llEmpty.visibility= View.GONE
//        }
    }

}